#include <string>
#include <boost/bind.hpp>
#include "../mars/thread_pool.h"
#include "../mars/blocking_queue.hpp"
#include "../mars/count_down_latch.h"
#include "../mars/logger.h"

#define THREAD_NUM 11 

using namespace mars;

BlockingQueue<std::string> cake_box;
CountDownLatch count_down_latch(THREAD_NUM);

void productor(const std::string& productor_name, int cake_count) {
    char cake_name[64];
    count_down_latch.count_down();
    for (int i = 0; i < cake_count; ++i) {
        snprintf(cake_name, 64, "%s_cake_%02d", productor_name.c_str(), i);
        INFO_PRINT("thread:%s productor cake_name:%s\n", CurrentThread::name(), cake_name);
        cake_box.put(std::string(cake_name));
        usleep(100);
    }    
}

void consumer(int consume_id) {
    count_down_latch.count_down();
    for (;;) {
        std::string cake_name = cake_box.take();
        INFO_PRINT("thread:%s consumer cake_name:%s\n", CurrentThread::name(), cake_name.c_str());
        usleep(10);
    }
}

int main() {
    ThreadPool pool;
    // 至少 11 个线程
    pool.start(THREAD_NUM);
    for (int i = 0; i < 10; ++i) {
        pool.run(boost::bind(consumer, i));
    }

    // 每个生产者生产 100 个蛋糕
    pool.run(boost::bind(productor, "Haagen-Dazs", 10));
    pool.run(boost::bind(productor, "Madeleine", 10));

    count_down_latch.wait();

    return 0;
}
